# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from wenlincms.core.fields import RichTextField

from wlmodels.codes.dicts import patent_class_dict, patent_state_dict, tmtype_dict
from wlmodels.codes.models import ELCC01, EntIdable
from wlmodels.company.models import ELPost
from .models_plus.statistics import *


# 招投标
class ELBid(EntIdable):
    REGNO = models.CharField("工商注册号", max_length=30, null=True)
    item_id = models.CharField('项目id', max_length=30, null=True)
    title = models.CharField("标题", max_length=128, null=True, blank=True)
    pubdate = models.CharField('发布日期', max_length=20, null=True, blank=True)
    case_type = models.CharField("公告类型", max_length=20, null=True, blank=True)
    case_buyer = models.CharField("采购人", max_length=20, null=True, blank=True)
    content = models.TextField("详情", null=True, blank=True)

    class Meta:
        verbose_name = '招标投标'
        verbose_name_plural = '招标投标'
        db_table = 'dataplus_bid'

    def __unicode__(self):
        return u'%s' % self.title

    def save(self, *args, **kwargs):
        if self.entid_id is None and self.REGNO:
            the_ents = list(ELPost.object.filter(REGNO=self.REGNO).values_list("entid", flat=True))
            if len(the_ents):
                self.entid_id = the_ents[0]
        super(ELBid, self).save(*args, **kwargs)


# 土地信息
class ELLand(EntIdable):
    REGNO = models.CharField("工商注册号", max_length=30, null=True)
    item_id = models.CharField('项目id', max_length=30, null=True)
    pubdate = models.CharField('发布时间', max_length=20, null=True, blank=True)
    case_type = models.CharField("公告类型", max_length=20, null=True, blank=True)
    district = models.CharField("行政区", max_length=30, null=True, blank=True)
    land_use = models.TextField("土地用途", null=True, blank=True)
    land_location = models.TextField("地块位置", null=True, blank=True)
    cover = models.CharField("土地面积", max_length=30, null=True, blank=True)
    content = models.TextField("土地详情", null=True, blank=True)

    class Meta:
        verbose_name = '土地信息'
        verbose_name_plural = '土地信息'
        db_table = 'dataplus_land'

    def __unicode__(self):
        return u'土地信息-%s' % self.pk

    def save(self, *args, **kwargs):
        if self.entid_id is None and self.REGNO:
            the_ents = list(ELPost.object.filter(REGNO=self.REGNO).values_list("entid", flat=True))
            if len(the_ents):
                self.entid_id = the_ents[0]
        super(ELLand, self).save(*args, **kwargs)


# 新闻事件
class ELNews(EntIdable):
    title = models.TextField('标题', null=True)
    body = RichTextField('内容', null=True, blank=True)
    srcurl = models.URLField('来源URL', null=True, blank=True)
    posttime = models.DateTimeField('时间', null=True, blank=True)
    ntype = models.CharField("新闻分类", max_length=5, null=True, blank=True, db_index=True,
                             choices=(("1", "分类1"), ("2", "分类2")), )
    ENTNAME = models.CharField("相关主体名称", max_length=128, null=True, blank=True)

    class Meta:
        verbose_name = '新闻事件'
        verbose_name_plural = '新闻事件'
        db_table = 'dataplus_news'

    def __unicode__(self):
        return u'%s' % self.title

    def save(self, *args, **kwargs):
        if self.entid_id is None and self.REGNO:
            the_ents = list(ELPost.object.filter(REGNO=self.REGNO).values_list("entid", flat=True))
            if len(the_ents):
                self.entid_id = the_ents[0]
        super(ELNews, self).save(*args, **kwargs)


# 招聘企业信息
class ELJobCompany(EntIdable):
    tyid = models.CharField('天元公司id', max_length=36, null=True, blank=True, db_index=True)
    name = models.CharField('公司名称', max_length=128, null=True, blank=True)
    registration_name = models.CharField('公司正式注册名称', max_length=128, null=True, blank=True)
    url = models.URLField('公司详情页地址', null=True, blank=True)
    overview = models.TextField('公司概述', null=True, blank=True)
    size_low = models.CharField('公司规模员工数下限', max_length=20, null=True, blank=True)
    size_high = models.CharField('公司规模员工数上限', max_length=20, null=True, blank=True)
    industries = models.CharField('所属行业', max_length=30, null=True, blank=True)
    ctype = models.CharField('公司类型', max_length=20, null=True, blank=True)
    contact = models.CharField('公司联系人', max_length=20, null=True, blank=True)
    phone = models.CharField('公司座机', max_length=20, null=True, blank=True)
    mobile = models.CharField('公司移动电话', max_length=20, null=True, blank=True)
    website = models.URLField('公司网站', null=True, blank=True)
    location = models.TextField('公司地址', null=True, blank=True)
    email = models.EmailField('公司邮箱地址', null=True, blank=True)
    qq = models.CharField('联系QQ', max_length=15, null=True, blank=True)
    first_run = models.CharField('第一次采集时间', max_length=20, null=True, blank=True)
    last_run = models.CharField('最后一次发现时间', max_length=20, null=True, blank=True)

    class Meta:
        verbose_name = '招聘企业信息'
        verbose_name_plural = '招聘企业信息'
        db_table = 'dataplus_job_company'

    def __unicode__(self):
        return u'%s' % self.name

    def save(self, *args, **kwargs):
        if self.entid_id is None and self.registration_name:
            the_ents = list(ELPost.object.filter(ENTNAME=self.registration_name).values_list("entid", flat=True))
            if len(the_ents):
                self.entid_id = the_ents[0]
        elif self.entid_id is None and self.name:
            the_ents = list(ELPost.object.filter(ENTNAME=self.name).values_list("entid", flat=True))
            if len(the_ents):
                self.entid_id = the_ents[0]
        super(ELJobCompany, self).save(*args, **kwargs)


# 招聘详细信息
class ELJobInfo(EntIdable):
    tyid = models.CharField('天元公司id', max_length=36, null=True, blank=True, db_index=True)
    job_id = models.CharField('职位ID', max_length=36, null=True, blank=True)
    job_title = models.CharField('职位名称', max_length=128, null=True, blank=True)
    url = models.URLField('对应网站中的详情页地址', null=True, blank=True)
    job_location = models.CharField('工作地址', max_length=30, null=True, blank=True)
    job_edu_levels = models.CharField('学历要求', max_length=30, null=True, blank=True)
    job_sal_type = models.CharField('薪资类型', max_length=20, null=True, blank=True)
    job_sal_low = models.CharField('工资下限', max_length=20, null=True, blank=True)
    job_sal_high = models.CharField('工资上限', max_length=20, null=True, blank=True)
    job_function = models.CharField('职能', max_length=30, null=True, blank=True)
    job_language = models.CharField('语言要求', max_length=30, null=True, blank=True)
    job_description = models.TextField('职位概述', null=True, blank=True)
    job_exp_high = models.CharField('工作经验上限', max_length=20, null=True, blank=True)
    job_exp_low = models.CharField('工作经验下限', max_length=20, null=True, blank=True)
    job_experiences = models.CharField('经验要求', max_length=30, null=True, blank=True)
    job_benefit = models.CharField('公司福利', max_length=100, null=True, blank=True)
    job_ben_annual = models.CharField('年假天数', max_length=10, null=True, blank=True)
    job_ben_social = models.CharField('社保', max_length=20, null=True, blank=True)
    job_ben_housing = models.CharField('公积金', max_length=20, null=True, blank=True)
    job_ben_commute = models.CharField('是否提供沟通和通勤补偿', max_length=10, null=True, blank=True)
    job_gender = models.CharField('性别要求', max_length=10, null=True, blank=True)
    job_age_low = models.CharField('年龄下限', max_length=10, null=True, blank=True)
    job_age_high = models.CharField('年龄上限', max_length=10, null=True, blank=True)
    job_contact = models.CharField('联系人', max_length=20, null=True, blank=True)
    job_con_phone = models.CharField('联系电话', max_length=20, null=True, blank=True)
    job_con_address = models.CharField('联系地址', max_length=100, null=True, blank=True)
    job_con_email = models.CharField('联系邮箱', max_length=75, null=True, blank=True)
    job_con_qq = models.CharField('联系QQ', max_length=75, null=True, blank=True)
    job_requirement = models.CharField('工作要求', max_length=300, null=True, blank=True)
    job_industry = models.CharField('所属行业', max_length=30, null=True, blank=True)
    job_hei_low = models.CharField('身高要求下限', max_length=20, null=True, blank=True)
    job_hei_high = models.CharField('身高要求上限', max_length=20, null=True, blank=True)
    job_wor_type = models.CharField('工作类型', max_length=20, null=True, blank=True)
    first_run = models.CharField('第一次采集时间', max_length=20, null=True, blank=True)
    last_run = models.CharField('最后一次发现时间', max_length=20, null=True, blank=True)

    class Meta:
        verbose_name = '招聘详情'
        verbose_name_plural = '招聘详情'
        db_table = 'dataplus_job_info'

    def __unicode__(self):
        return u'%s' % self.job_title

    def save(self, *args, **kwargs):
        if self.entid_id is None and self.tyid:
            the_ents = list(ELJobCompany.object.filter(tyid=self.tyid).values_list("entid_id", flat=True))
            if len(the_ents) and the_ents[0]:
                self.entid_id = the_ents[0]
        super(ELJobInfo, self).save(*args, **kwargs)


# 专利信息
class ELPatent(ELBaseModel):
    pid = models.IntegerField('专利ID', primary_key=True)
    sqh = models.CharField('专利申请号', max_length=36, null=True)
    patname = models.CharField('专利标题', max_length=200, null=True, blank=True)
    sqr = models.CharField('申请/专利权人', max_length=200, null=True, blank=True)
    sqrq = models.CharField('申请日期', max_length=30, null=True, blank=True)
    gkggh = models.CharField('公开/公告号', max_length=36, null=True, blank=True)
    fmr = models.TextField('发明/设计人', null=True, blank=True)
    gkggr = models.CharField('公开/公告日', max_length=30, null=True, blank=True)
    fcfl = models.CharField('范畴分类', max_length=50, null=True, blank=True)
    flh = models.TextField('分类号', null=True, blank=True)
    bzrq = models.CharField('颁证日期', max_length=30, null=True, blank=True)
    dlr = models.CharField('代理人', max_length=200, null=True, blank=True)
    dz = models.CharField('地址', max_length=200, null=True, blank=True)
    fasq = models.CharField('分案申请', max_length=200, null=True, blank=True)
    gjgb = models.CharField('国际公布', max_length=200, null=True, blank=True)
    gjsq = models.CharField('国际申请', max_length=200, null=True, blank=True)
    gsdm = models.CharField('国省代码', max_length=30, null=True, blank=True)
    jrgjrq = models.CharField('进入国家日期', max_length=30, null=True, blank=True)
    tzzl = models.TextField('同族专利', null=True, blank=True)
    yxq = models.TextField('优先权', null=True, blank=True)
    yzwx = models.TextField('引证文献', null=True, blank=True)
    zflh = models.CharField('主分类号', max_length=50, null=True, blank=True)
    zldljg = models.CharField('专利代理机构', max_length=150, null=True, blank=True)
    zqx = models.TextField('主权项', null=True, blank=True)
    zy = models.TextField('摘要', null=True, blank=True)

    class Meta:
        verbose_name = '专利信息'
        verbose_name_plural = '专利信息'
        db_table = 'dataplus_patent'

    def __unicode__(self):
        return u'%s' % self.sqh


# 专利所属企业
class ELPatentEnt(EntIdable):
    pid = models.ForeignKey('dataplus.ELPatent', verbose_name='专利id', db_column='pid')

    class Meta:
        verbose_name = '专利所属企业'
        verbose_name_plural = '专利所属企业'
        db_table = 'dataplus_patent_ent'

    def __unicode__(self):
        return u'%s' % self.id


# 软件著作权
class ELCopyrightSoftware(ELBaseModel):
    entid = models.IntegerField('企业ID', max_length=11, null=False)
    softname = models.CharField('软件名称', max_length=100, null=True)
    regno_software = models.TextField('登记号', null=True, blank=True)
    classno_software = models.TextField('分类号', null=True, blank=True)
    softname_full = models.TextField('软件全称', null=True, blank=True)
    softname_short = models.TextField('软件简称', null=True, blank=True)
    softverson = models.TextField('版本号', null=True, blank=True)
    copyright_owner_software = models.TextField('著作权人', null=True, blank=True)
    firstpubdate_software = models.DateField('首次发表日期', null=True, blank=True)
    regdate_software = models.DateField('登记日期', null=True, blank=True)

    class Meta:
        verbose_name = '软件著作权'
        verbose_name_plural = '软件著作权'
        db_table = 'dataplus_copyright_software'

    def __unicode__(self):
        return u'%s' % self.softname


# 作品著作权
class ELCopyrightProduction(ELBaseModel):
    entid = models.IntegerField('企业ID', max_length=11, null=False)
    prodname = models.CharField('作品名称', max_length=100)
    regno_production = models.TextField('登记号', null=True, blank=True)
    regdate_production = models.DateField('登记日期', null=True, blank=True)
    class_production = models.TextField('作品类别', null=True, blank=True)
    copyright_owner_production = models.TextField('著作权人', null=True, blank=True)
    creadate_production = models.DateField('创作完成日期', null=True, blank=True)
    firstpubdate_production = models.DateField('首次发表日期', null=True, blank=True)

    class Meta:
        verbose_name = '作品著作权'
        verbose_name_plural = '作品著作权'
        db_table = 'dataplus_copyright_production'

    def __unicode__(self):
        return u'%s' % self.prodname


# ICP备案登记信息
class ELIcpreg(ELBaseModel):
    regno_ICP = models.CharField('备案号或许可证号', max_length=50, unique=True)
    name_ICP = models.TextField('主办单位', null=True, blank=True)
    type_ICP = models.TextField('性质', null=True, blank=True)
    webname_ICP = models.TextField('网站名称', null=True, blank=True)
    url_ICP = models.TextField('首页地址', null=True, blank=True)
    audited_date_ICP = models.DateField('审核时间', null=True, blank=True)

    class Meta:
        verbose_name = 'ICP备案登记'
        verbose_name_plural = 'ICP备案登记'
        db_table = 'dataplus_icpreg'

    def __unicode__(self):
        return u'%s' % self.regno_ICP


# 商标信息
class ELTminfo(EntIdable):
    RegNO = models.CharField('申请/注册号', max_length=50, null=True, db_index=True)
    TMApplicant = models.CharField('申请人', max_length=200, null=True, blank=True, db_index=True)
    IntCls_id = CrossForeignCharField('国际商标分类', db_column='IntCls', max_length=4, null=True, blank=True)
    TMCN = models.CharField('商标名称（中文）', null=True, blank=True, max_length=130)
    TMEN = models.TextField('商标名称（英文）', null=True, blank=True, max_length=130)
    AppDate = models.DateField('申请日期', null=True, blank=True)
    TrialDate = models.DateField('初审公告日期', null=True, blank=True)
    RegDate = models.DateField('注册日期', null=True, blank=True)
    InterRegDate = models.DateField('国际注册日期', null=True, blank=True)
    ValidDate = models.DateField('失效日期', null=True, blank=True)
    TrialNum = models.CharField('初审序号', null=True, blank=True, max_length=20)
    RegNum = models.CharField('注册序号', null=True, blank=True, max_length=20)
    TMAddress = models.TextField('申请人地址', null=True, blank=True)
    TMAgent = models.CharField('代理人', null=True, blank=True, max_length=200)
    TMDetail = models.TextField('商标类群详情', null=True, blank=True)
    SimilarGroup = models.TextField('类似群', null=True, blank=True)
    TMType = models.CharField('商标类型', null=True, blank=True, max_length=8, choices=tmtype_dict)
    ISTotal = models.CharField('ISTotal', null=True, blank=True, max_length=2)
    TMAreaNum = models.CharField('商标地区号', null=True, blank=True, max_length=20)
    TMRemark = models.TextField('备注', null=True, blank=True)
    TMStatus = models.TextField('商标状态', null=True, blank=True)
    TMZT = models.CharField('TMZT', null=True, blank=True, max_length=20)
    TMSZ = models.CharField('TMSZ', null=True, blank=True, max_length=20)

    class Meta:
        verbose_name = '商标信息'
        verbose_name_plural = '商标信息'
        db_table = 'dataplus_tminfo'

    def __unicode__(self):
        return u'商标-%s' % self.id

    def _intcls(self):
        the_intcls = list(ELCC01.objects.filter(code=self.IntCls_id).values_list("name", flat=True))
        if len(the_intcls):
            return the_intcls[0]
        return ""

    _intcls.short_description = "国际商标分类"

    IntCls = property(_intcls)
# 商标流程信息
class ELTmflow(ELBaseModel):
    RegNO = models.CharField('申请/注册号', max_length=50, null=True, db_index=True)
    IntCls_id = CrossForeignCharField('国际商标分类', db_column='IntCls', max_length=4, null=True, blank=True)
    FlowName = models.TextField('流程名称', null=True, blank=True)
    FlowDate = models.DateField('流程日期', null=True, blank=True)

    class Meta:
        verbose_name = '商标流程信息'
        verbose_name_plural = '商标流程信息'
        db_table = 'dataplus_tmflow'

    def __unicode__(self):
        return u'商标流程-%s' % self.id

    def _intcls(self):
        the_intcls = list(ELCC01.objects.filter(code=self.IntCls_id).values_list("name", flat=True))
        if len(the_intcls):
            return the_intcls[0]
        return ""

    _intcls.short_description = "国际商标分类"

    IntCls = property(_intcls)


# 学历信息
class ELDegreeinfo(ELBaseModel):
    name = models.CharField('姓名', max_length=20, null=True)
    id_card = models.CharField('身份证号码', max_length=30, null=True, db_index=True)
    graddate = models.DateField('毕业时间', null=True, blank=True)
    enrodate_year = models.DateField('入学年份', null=True, blank=True)
    gradschool = models.TextField('毕业院校', null=True, blank=True)
    gradresult = models.TextField('毕业结论', null=True, blank=True)
    degreetype = models.TextField('学历类型', null=True, blank=True)
    educdegree = models.TextField('学历', null=True, blank=True)
    photo_degree = models.TextField('照片', null=True, blank=True)
    major = models.TextField('专业', null=True, blank=True)

    class Meta:
        verbose_name = '个人学历信息'
        verbose_name_plural = '个人学历信息'
        db_table = 'dataplus_degreeinfo'

    def __unicode__(self):
        return u'%s的学历信息' % self.id_card


# 引入子model的虚变量，以防import被优化
models_imported = [models_statistics_imported, ]
